'use strict';

const Controller = require('egg').Controller;

class MessagesController extends Controller {

    constructor(ctx) {
        super(ctx);
        // this.Rule = {
        //   accesstoken: 'string',
        //   title: 'string',
        //   content: 'string',
        // };
    } 

    async index() {
        const { ctx } = this;
        const { group, userid, type } = ctx.query;
        let messages;
        if (type === 'userid') {
            messages = await ctx.service.messages.getMessageByUserid(userid);  
        } else if (type === 'group') {
            messages = await ctx.service.messages.getMessageByGroup(group);  
        } else {
            messages = await ctx.service.messages.getMessageByReply(userid);  
        }
        
        ctx.status = 200;
        ctx.body = messages
    }

    async new() {
        const { ctx } = this;
        const { messageType } = ctx.query;
        
        if (messageType === 'send' || messageType === 'reply') {
            const res = await ctx.service.messages.newMessage(messageType, ctx.query);
            ctx.status = 201;

        } else {
            ctx.status = 409;
        }        
    }
}

module.exports = MessagesController;